package cn.tedu.csmall.product.controller;


import cn.tedu.csmall.product.pojo.dto.CategoryAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.CategoryUpdateDTO;
import cn.tedu.csmall.product.pojo.vo.BrandListItemVO;
import cn.tedu.csmall.product.pojo.vo.CategoryListItemVO;
import cn.tedu.csmall.product.pojo.vo.CategoryStandardVO;
import cn.tedu.csmall.product.service.ICategoryService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 处理分类相关请求的控制器
 *
 * @author 何惠民
 * @version 0.0.1
 */
@Api(tags = "03分类管理模块")
@Slf4j
@RestController
@RequestMapping("/categories")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    public CategoryController() {
        log.debug("创建控制器对象:CategoryController");
    }

    @ApiOperation("添加分类")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public String  addNewCategory(CategoryAddNewDTO categoryAddNewDTO){
        log.debug("开始处理[添加分类]的请求,参数{}",categoryAddNewDTO);
        categoryService.addNew(categoryAddNewDTO);
        log.debug("添加测试数据成功");
        return "添加分类成功";
    }

    @ApiOperation("根据id删除分类")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "分类id", required = true ,dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult deleteById(@Range(min = 1, message = "删除分类失败,删除的分类ID无效")
                                 @PathVariable Long id){
        log.debug("开始处理[删除分类]的请求,参数:{}",id);
        categoryService.deleteById(id);
        log.debug("删除Id为{}的分类成功",id);
        return JsonResult.ok();
    }

    @ApiOperation("修改分类")
    @ApiOperationSupport(order = 300)
    @PostMapping("/update")
    public JsonResult<Void>  update(CategoryUpdateDTO categoryUpdateDTO){
        log.debug("开始处理[修改分类]的请求,参数{}",categoryUpdateDTO);
        categoryService.update(categoryUpdateDTO);
        log.debug("修改测试数据成功");
        return JsonResult.ok();
    }

    @ApiOperation("启用分类")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "分类id", required = true ,dataType = "long")
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult setEnable(@Range(min = 1, message = "启用分类失败,启用的分类ID无效")
                                @PathVariable Long id){
        log.debug("开始处理[启用分类]的请求,参数id={}",id);
        categoryService.setEnable(id);
        return JsonResult.ok();
    }
    @ApiOperation("禁用分类")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParam(name = "id", value = "分类id", required = true ,dataType = "long")
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult setDisable(@Range(min = 1, message = "禁用分类失败,禁用的分类ID无效")
                                 @PathVariable Long id){
        log.debug("开始处理[禁用分类]的请求,参数id={}",id);
        categoryService.setDisable(id);
        return JsonResult.ok();
    }

    @ApiOperation("显示类别")
    @ApiOperationSupport(order = 312)
    @ApiImplicitParam(name = "id", value = "类别id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/display")
    public JsonResult<Void> setDisplay(@PathVariable Long id) {
        log.debug("开始处理【显示类别】的请求：id={}", id);
        categoryService.setDisplay(id);
        return JsonResult.ok();
    }

    @ApiOperation("隐藏类别")
    @ApiOperationSupport(order = 313)
    @ApiImplicitParam(name = "id", value = "类别id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/hidden")
    public JsonResult<Void> setHidden(@PathVariable Long id) {
        log.debug("开始处理【隐藏类别】的请求：id={}", id);
        categoryService.setHidden(id);
        return JsonResult.ok();
    }

    @ApiOperation("根据id查询类别详情")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id", value = "类别id", required = true, dataType = "long")
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<CategoryStandardVO> getStandardById(@PathVariable Long id) {
        log.debug("开始处理[根据id查询分类]的请求,参数{}",id);
        CategoryStandardVO categoryStandardVO = categoryService.getStandardById(id);
        log.debug("查询分类成功查询到{}",categoryStandardVO);
        return JsonResult.ok(categoryStandardVO);
    }


    @ApiOperation("查询分类列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<CategoryListItemVO>> list(){
        log.debug("开始处理[查询分类列表]的请求,参数无");
        List<CategoryListItemVO> list = categoryService.list();
        log.debug("查询分类列表成功");
        return JsonResult.ok(list);
    }

    @ApiOperation("根据父类级别插叙子集列表")
    @ApiOperationSupport(order = 430)
    @GetMapping("/list-by-parent")
    public JsonResult<List<CategoryListItemVO>> listByparentId(Long parentId){
        log.debug("开始处理[根据父类级别插叙子集列表]的请求,参数无");
        List<CategoryListItemVO> list = categoryService.listByparentId(parentId);
        log.debug("查询分类列表成功");
        return JsonResult.ok(list);
    }
}
